package com.chixing.day16_io.reflect;



import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/*
    A.java---->A.class
    通过字节码文件得到类的结构
    Student:  成员变量       构造函数      方法      父类       接口         注解
            （字段field）（Constructor）（Methed） superCalss  interface  annotations
    Methed：修饰符 + 返回值 + 方法名 + 参数列表 + 异常
    修饰符（public+abstract+static + final + native

*/
public class ReflectDemo {
    public static void main(String[] args) throws IllegalAccessException {
        Class<Student> clasz = Student.class;//反射

        String className = clasz.getName();
        System.out.println("类名"+className);
        clasz.getAnnotations();//注解






        Student stu1 = null;
        try {
            stu1 = clasz.newInstance();//依赖于无参构造函数
            System.out.println("实例化Student对象");
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        }


        clasz.getMethods();
        //1.获得所以的方法
        Method[] methods = clasz.getDeclaredMethods();
        for (Method method : methods){
            int modfider = method.getModifiers();
            String methoName = method.getName();
            Class<?> returnType = method.getReturnType();
            Class<?>[] paramTypes = method.getParameterTypes();
            Class<?>[] exceptionType = method.getExceptionTypes();


            System.out.print("访问修饰符"+modfider);
            System.out.print(" 方法名称"+methoName);
            System.out.print(" 返回值类型"+returnType);
            for (Class<?> paramType:paramTypes){
                System.out.print(" 参数列表类型:"+paramType);
            }
            for (Class<?> exType:exceptionType){
                System.out.print(" 异常类型"+exType);
            }
            System.out.println();
        }

        //方法调用 stu1.play(int,float)
        Method method = null;
        try {
            method = clasz.getMethod("play", int.class, float.class);
        } catch (NoSuchMethodException e) {
            throw new RuntimeException(e);
        }
        //arguments实参
        //result:result value
        Object result = null;
        try {
            result = method.invoke(stu1,10,9.9f);
            System.out.println("play方法的返回值"+result);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        }
        //方法调用 类.方法 : Object result = Student.study(...)

        try {
            Method method1 = clasz.getMethod("study", int.class, String.class, Float.class);
            try {
                Object result1 = method1.invoke(Student.class,5,"6",1.2f);
                System.out.println("类调用方法返回值："+result1);
            } catch (InvocationTargetException e) {
                throw new RuntimeException(e);
            }
        } catch (NoSuchMethodException e) {
            throw new RuntimeException(e);
        }


        //2.获得所有的字段
        Field[] fields = clasz.getFields();
        for (Field field:fields){
            String fieldName = field.getName();
            Class<?> fieldType = field.getType();
            int fieldMo = field.getModifiers();


            System.out.print("字段修饰符" +fieldMo);
            System.out.print(" 字段类型"+fieldType);
            System.out.print(" 字段名称"+fieldName);


            System.out.println();
        }


    }
}
